Hello World (the Cross Compiler Sanity Check)
Now that you have build the cross compilation toolchain, you should be able to build C programs for RISCV.
The Program
The one you all know and love:
#include <stdio.h>
int main() {
printf("Hello World?\n");
return 0;
}
The Compilation
In order to compile the example to RISCV, there are a few steps:
- Create an object file with clang:
clang -O -c hello.c --target=riscv-unknown-elf -march=rv64gcv
- This is where the heavy lifting happens, so add all your desired flags here
- Compile into an executable with gcc:
riscv-unknown-elf-gcc hello.o -o hello
- You'll find this executable in your install directory for the riscv toolchain.
- Run the executable:
gem5 simple.py --binary hello
Note that a better sanity check is probably to run this through a qemu vm, but since we have gem5 (and this course is based on gem5), might as well use it.
sidebar
QEMU is incredibly powerful. If you can get the hang of the command line, you will be able to simulate anything for execution. QEMU, unlike gem5, is an emulator. It does not provide cycle-accurate analysis, but it can run production hardware emulating a target system such that you can interact and debug much easier than with gem5. If you're curious, go and read about the QEMU project.